package com.goods.business.service.imp;

import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public Map findSupplierList(int pageNum, int pageSize, String name,String address,String contact) {

        List<Supplier> supplierList = supplierMapper.selectByAddress(name,address,contact);
//        List<Supplier> supplierList = supplierMapper.selectAll();

        //转成vo
        List<SupplierVO> supplierVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(supplierList)){
            for (Supplier supplier : supplierList) {
                SupplierVO supplierVO = new SupplierVO();
                BeanUtils.copyProperties(supplier,supplierVO);
                supplierVOS.add(supplierVO);
            }
        }
        List<SupplierVO> page = ListPageUtils.page(supplierVOS, pageSize, pageNum);
        int total = supplierVOS.size();
        Map map = new HashMap();
        map.put("rows",page);
        map.put("total",total);

        return map;
    }

    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }

    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }

    @Override
    public void updateById(Long id,SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

    @Override
    public int deleteById(Long id) {
        int i = supplierMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public List<SupplierVO> findAll() {

        List<Supplier> supplierList = supplierMapper.selectAll();

        //转成vo
        List<SupplierVO> supplierVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(supplierList)){
            for (Supplier supplier : supplierList) {
                SupplierVO supplierVO = new SupplierVO();
                BeanUtils.copyProperties(supplier,supplierVO);
                supplierVOS.add(supplierVO);
            }
        }

        return supplierVOS;
    }
}
